import os
from pathlib import Path
from environs import Env
from tortoise import Tortoise, run_async


# 初始化数据库至models

root_path = Path(__file__).resolve(strict=True).parent.parent.parent

env = Env()
if os.path.exists(f"{root_path}/.env"):
    env.read_env(f"{root_path}/.env")
# Mysql版
DB_URI = env.str("DATABASE_URL", None)

# SQLite版
# DB_URI = f'sqlite:///fast.db'


async def init():
    # Here we create a SQLite DB using file "db.sqlite3"
    #  also specify the app name of "models"
    #  which contain models from "app.models"
    await Tortoise.init(
        db_url=DB_URI,
        modules={'models': ['models']}
    )
    # Generate the schema
    await Tortoise.generate_schemas()

# run_async is a helper function to run simple async Tortoise scripts.
run_async(init())

# 迁移语句
# aerich migrate --name chat_exclusive_key
# 执行迁移
# aerich upgrade
